function getQueryParam(name, queryString) {
    var match = RegExp(name + '=([^&]*)').exec(queryString || location.search);
    return match && decodeURIComponent(match[1]);
}

function hasOption(opt) {
    var s = window.location.search;
    var re = new RegExp('(?:^|[&?])' + opt + '(?:[=]([^&]*))?(?:$|[&])', 'i');
    var m = re.exec(s);

    return m ? (m[1] === undefined ? true : m[1]) : false;
}

var scriptTags = document.getElementsByTagName('script'),
    defaultTheme = 'neptune',
    defaultRtl = false,
    i = scriptTags.length,
    requires = [
        'Ext.toolbar.Toolbar',
        'Ext.form.field.ComboBox',
        'Ext.form.FieldContainer',
        'Ext.form.field.Radio'

    ],
    defaultQueryString, src, theme, rtl;

while (i--) {
    src = scriptTags[i].src;
    if (src.indexOf('include-ext.js') !== -1) {
        defaultQueryString = src.split('?')[1];
        if (defaultQueryString) {
            defaultTheme = getQueryParam('theme', defaultQueryString) || defaultTheme;
            defaultRtl = getQueryParam('rtl', defaultQueryString) || defaultRtl;
        }
        break;
    }
}

Ext.themeName = theme = getQueryParam('theme') || defaultTheme;

rtl = getQueryParam('rtl') || defaultRtl;

if (rtl.toString() === 'true') {
    requires.push('Ext.rtl.*');
    Ext.define('Ext.GlobalRtlComponent', {
        override: 'Ext.AbstractComponent',
        rtl: true
    });
}

Ext.require(requires);

Ext.onReady(function() {
    Ext.getBody().addCls(Ext.baseCSSPrefix + 'theme-' + Ext.themeName);

    if (Ext.isIE6 && theme === 'neptune') {
        Ext.Msg.show({
            title: 'Browser Not Supported',
            msg: 'The Neptune theme is not supported in IE6.',
            buttons: Ext.Msg.OK,
            icon: Ext.Msg.WARNING
        });
    }
    
    if (hasOption('nocss3')) {
        Ext.supports.CSS3BorderRadius = false;
        Ext.getBody().addCls('x-nbr x-nlg');
    }
    function setParam(param) {
        var queryString = Ext.Object.toQueryString(
            Ext.apply(Ext.Object.fromQueryString(location.search), param)
        );
        location.search = queryString;
    }

    function removeParam(paramName) {
        var params = Ext.Object.fromQueryString(location.search);

        delete params[paramName];

        location.search = Ext.Object.toQueryString(params);
    }

    var toolbar;
        
    setTimeout(function() {
        toolbar = Ext.widget({
            xtype: 'toolbar',
            border: true,
            rtl: false,
            id: 'options-toolbar',
            width   : 500,
            floating: true,
            fixed: true,
            preventFocusOnActivate: true,
            draggable: {
                constrain: true
            },
            items: [
            {
                xtype: 'label',
                id: 'label_time',
                style:'fontWeight: bold;'
            },
            {
                xtype: 'combo',
                rtl: false,
                width: 170,
                labelWidth: 45,
                fieldLabel: '风格',
                labelAlign: 'right',
                displayField: 'name',
                valueField: 'value',
                labelStyle: 'cursor:move;',
                //margin: '0 5 0 0',
                store: Ext.create('Ext.data.Store', {
                    fields: ['value', 'name'],
                    data : [
                        { value: 'access', name: 'Accessibility' },
                        { value: 'classic', name: 'Classic' },
                        { value: 'gray', name: 'Gray' },
                        { value: 'neptune', name: 'Neptune' }
                    ]
                }),
                value: theme,
                listeners: {
                    select: function(combo) {
                        var theme = combo.getValue();
                        if (theme !== defaultTheme) {
                            setParam({ theme: theme });
                        } else {
                            removeParam('theme');
                        }
                    }
                }
            }, {
                xtype: 'button',
                rtl: false,
                hidden: !(Ext.repoDevMode || location.href.indexOf('qa.sencha.com') !== -1),
                enableToggle: true,
                pressed: rtl,
                text: 'RTL',
                margin: '0 5 0 0',
                listeners: {
                    toggle: function(btn, pressed) {
                        if (pressed) {
                            setParam({ rtl: true });
                        } else {
                            removeParam('rtl');
                        }
                    }
                }
            }, {
                xtype:'button',
                rtl: false,
                text: '退出系统',
                icon: '../img/logout.gif',
                handler:function() {
                    Ext.MessageBox.confirm('温馨提示','您确认退出系统吗？',function(val){
                        if(val == 'yes') {
                            Ext.Ajax.request({
                                url: '/admin_user/logout',
                                method : 'GET',
                                success: function(response, opts) {
                                    var data = Ext.JSON.decode(response.responseText);
                                    window.location.href = '/login.html';
                                    // if(data.success){
                                    //     window.location.href = '/login.html';
                                    // } else {
                                    //     window.location.href = '/login.html';
                                    // }
                                },
                                failure: function(response, opts) {
                                    var data = eval("("+response.responseText+")");
                                    if(!data.success){
                                        Ext.Msg.alert('信息提示',data.msg);
                                    } else {
                                        Ext.Msg.alert("信息提示","网络错误，请稍候再试！");
                                    }
                                }
                            });
                        }
                    });
                }
            }, {
                xtype: 'tool',
                type: 'close',
                rtl: false,
                handler: function() {
                    toolbar.destroy();
                }
            }],

            // Extra constraint margins within default constrain region of parentNode
            constraintInsets: '0 -' + (Ext.getScrollbarSize().width + 4) + ' 0 0'
        });
        toolbar.show();
        toolbar.alignTo(
            document.body,
            Ext.optionsToolbarAlign || 'tr-tr',
            [
                (Ext.getScrollbarSize().width + 4) * (Ext.rootHierarchyState.rtl ? 1 : -1),
                -(document.body.scrollTop || document.documentElement.scrollTop)
            ]
        );
        
        var constrainer = function() {
            toolbar.doConstrain();
        };
        
        Ext.EventManager.onWindowResize(constrainer);
        toolbar.on('destroy', function() { 
            Ext.EventManager.removeResizeListener(constrainer);
        });

        var showTime = function() {
            //console.log(Ext.getCmp('label_time'));
            //console.log(moment().format('YYYY年MM月DD日 HH:mm:ss'));
            Ext.getCmp('label_time').setText('当前时间：'+moment().format('YYYY年MM月DD日 HH:mm:ss'));
            //document.getElementById('label_time').innerHTML = '当前时间：'+moment().format('YYYY年MM月DD日 HH:mm:ss');
            setTimeout(showTime,1000);
        }
        showTime();
    }, 50);
});